<template>
	<view class="container">
		<view class="header">
			<view class="header-title">
<!--				<view class="header-icon">-->
<!--          <image :src="ORDER_STATUS_ICON[order.status]"></image>-->
<!--				</view>-->
				<view class="header-status">
					<text>{{ ORDER_STATUS_TEXT[order.status] }}</text>
				</view>
			</view>
			<view class="header-content" v-if="order.status === ORDER_STATUS.PENDING">
				<text>{{ expreTime }}分钟后订单关闭，请及时付款哦</text>
			</view>
			<view class="header-empty"></view>
		</view>
		<view class="content">
			<!-- 地址信息 -->
<!--			<view class="address">-->
<!--				<view class="address-left">-->
<!--					<image src="/static/icon/address.png"></image>-->
<!--				</view>-->
<!--				<view class="address-right">-->
<!--					<view class="address-info">-->
<!--						<view class="address-info-top">-->
<!--							{{ order.address || '' }}-->
<!--						</view>-->
<!--						<view class="address-info-bottom">-->
<!--							<text class="address-name">{{ order.address_nickname || ''}}</text>-->
<!--							<text class="address-phone">-{{ order.address_phone || ''}}</text>-->
<!--						</view>-->
<!--					</view>-->
<!--&lt;!&ndash;					<view class="address-righticon">&ndash;&gt;-->
<!--&lt;!&ndash;						<image src="/static/icon/right.png"></image>&ndash;&gt;-->
<!--&lt;!&ndash;					</view>&ndash;&gt;-->
<!--				</view>-->
<!--			</view>-->
			<!-- 商品信息 -->
			<view class="goods" v-for="item in items" @click="toGoodsDetail(item.product.ticket_shop_id)">
				<view class="goods-center">
					<text class="goods-name">{{ item.product.title || '' }}</text>
					<text class="goods-num">数量：{{ item.count || 0 }} {{ item.product.unit || '' }}</text>
				</view>
				<view class="goods-right">
					￥<text class="goods-price">{{ item.amount }}</text>
				</view>
			</view>
			<!-- 价格信息 -->
			<view class="price">
				<view class="price-top">
					<view class="price-top-xiaoji">
						<text class="price-top-left">总金额</text>
						<view class="price-top-right">
							￥<text class="xiaoji-price">{{ order.amount }}</text>
						</view>
					</view>
					<view class="price-top-youhui" v-if="order.discount_amount">
						<text class="price-top-left">优惠券</text>
						<text class="youhui-price">-￥{{ order.discount_amount }}</text>
					</view>
          <view class="price-top-youhui" v-if="order.balance_deduct">
            <text class="price-top-left">余额支付</text>
            <text class="youhui-price">-￥{{ order.balance_deduct }}</text>
          </view>
				</view>
				<view class="price-bottom">
					<text class="price-bottom-left">实际支付</text>
					<view class="price-bottom-right">
						￥<text class="xiaoji-price">{{ order.payment_amount }}</text>
					</view>
				</view>
			</view>
			<!-- 订单信息 -->
			<view class="order">
				<view class="order-item">
					<text class="order-item-left">下单时间</text>
					<text class="order-item-right">{{ order.create_time }}</text>
				</view>
				<view class="order-item">
					<text class="order-item-left">订单编号</text>
					<text class="order-item-right">{{ order.order_no }}</text>
				</view>
				<view class="order-item">
					<text class="order-item-left">支付方式</text>
					<text class="order-item-right">微信</text>
				</view>
				<view class="order-item" v-if="order.transaction_id">
					<text class="order-item-left">交易流水号</text>
					<text class="order-item-right">{{ order.transaction_id || '' }}</text>
				</view>
			</view>
		</view>

		<!-- 底部栏 -->
		<view class="bottom">
			<view class="btns">
				<button class="cancel" v-if="order.statusText=='待支付'" @click="cancelOrder()">取消订单</button>
<!--				<button class="cancel" v-if="order.statusText=='待收货' || order.statusText=='待评价'">再来一单</button>-->
				<button class="pay" v-if="order.statusText=='待支付'" @click="payment(order.id)">支付</button>
<!--				<button class="pay" v-if="order.statusText=='已支付'" @click="remind()">提醒发货</button>-->
<!--				<button class="pay" v-if="order.statusText=='待收货'">确认收货</button>-->
<!--				<button class="cancel" v-if="order.statusText=='待评价' && !order.comments">退款/售后</button>-->
<!--				<button class="pay" v-if="order.statusText=='待评价' && !order.comments" @click="goComment()">去评价</button>-->
			</view>
		</view>
	</view>
</template>

<script>
  import { ORDER_STATUS, ORDER_STATUS_TEXT, ORDER_STATUS_ICON } from "../../utils/enum";
  import dayjs from "dayjs";

  export default {
		data() {
			return {
				order: {
					status: "待支付"
				},
        ORDER_STATUS,
        ORDER_STATUS_TEXT,
        ORDER_STATUS_ICON,
        expreTime: 0,
        items: [],
        interval: null
			}
		},
    onLoad({ orderId }) {
      if (!this.$store.state.userInfo.id) {
        return
      }
      this.$api.getOrderDetail(orderId).then(res => {
        let _res = res || { comments: [] }
        this.order = {
          ..._res,
          statusText: ORDER_STATUS_TEXT[_res.status],
          comments: _res.comments.length || 0
        }
        if (this.order.status === ORDER_STATUS.PENDING && this.order.time_expire) {
          this.interval = setInterval(() => {
            this.getExpireTime()
          },1000)
        }
        if (this.order.items && this.order.items.length) {
          this.items = this.order.items.map(item => {
            let cartItem = {}
            if (item.product_detail) {
              cartItem = JSON.parse(item.product_detail)
            }
            return {
              ...item,
              product: cartItem,
              amount: item.count * cartItem.price
            }
          }).filter(item => item.id)
        }

        console.log(this.order)
      })
    },
		methods: {
      // 计算订单自动关闭时间与现在差多久
      getExpireTime() {
        // this.order.time_expire 是年月日 时分秒，使用dayjs判断多分钟后过期
        this.expreTime = dayjs(this.order.time_expire).diff(dayjs(), 'minute')
        if (this.expreTime <= 0) {
          this.$api.cancelOrder({ id: this.order.id })
          clearInterval(this.interval)
          // 退出
          uni.reLaunch({
            url: '/pages/order/order'
          })
        }
      },
      toGoodsDetail(id) {
        uni.navigateTo({
          url: "/pages/goods/goods?id=" + id
        })
      },
			// 跳转去评价页面
			goComment() {
				uni.navigateTo({
					url: "/pages/order/comment?orderId=" + this.order.id
				})
			},
      async payment(order_id){
        await this.$api.getOrderDetail(order_id)
        uni.showLoading({
          title:'微信支付请求'
        });
        const that = this
        this.$api.createPay({
          order_id
        }).then(payment =>{
          uni.requestPayment({
            provider: 'wxpay',
            timeStamp: payment.timestamp,
            nonceStr: payment.nonce_str,
            package: payment.package,
            signType: payment.signType,
            paySign: payment.paySign,
            success:(result)=> {
              uni.showToast({
                title:'支付成功'
              });
            },
            fail: (result) => {
              uni.showToast({
                icon:'none',
                title:'支付失败',
              });
            },
            complete: () => {
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/cart/paymentinfo?orderId=" + order_id
                })
              }, 1000);
            }
          });
        }).catch(res=>{
          this.loading = false;
          setTimeout(() => {
            uni.navigateTo({
              url: "/pages/cart/paymentinfo?orderId=" + order_id
            })
          }, 1000);
        })
      },
			// 提醒发货
			remind() {
				this.$utils.toastTip("已向商家发送发货提醒");
			},
			// 取消订单
			cancelOrder() {
				this.$utils.showConfirm("确定要取消订单吗？").then(res => {
					if (res.confirm) {
            // 调用取消订单API
            uni.showLoading({
              title: '取消中...'
            })
            this.$api.cancelOrder({
              id: this.order.id
            }).then(res => {
              this.$utils.toastTip("取消成功！");
              this.order.status = ORDER_STATUS.CANCELLED
              this.order.statusText = ORDER_STATUS_TEXT[ORDER_STATUS.CANCELLED]
            })
					}
				});

			}
		}
	}
</script>

<style>
	page {
		background: #F8F9FA;
	}
</style>

<style lang="scss" scoped>
	.header {
		border-bottom-left-radius: 60% 15%;
		border-bottom-right-radius: 60% 15%;
    background: #4e8ef7;
		color: #FEFFFE;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 50rpx 0rpx 0;


		.header-title {
			display: flex;
			align-items: center;

			.header-icon {
				image {
					width: 80rpx;
					height: 80rpx;
				}
			}

			.header-status {
				font-size: 36rpx;
				margin-left: 16rpx;
			}
		}

		.header-content {
			margin-top: 16rpx;
		}

		.header-empty {
			padding-bottom: 100rpx;
		}

	}

	.content {
		margin: 0rpx 30rpx;

		.address {
			display: flex;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 32rpx 20rpx;
			margin-top: -100rpx;

			.address-left {

				image {
					width: 48rpx;
					height: 48rpx;
				}
			}

			.address-right {
				display: flex;
				align-items: center;

				.address-info {
					display: flex;
					flex-direction: column;
					padding: 0rpx 40rpx 0rpx 20rpx;

					.address-info-top {
						font-size: 30rpx;
						font-weight: 600;
						color: #333333;
					}

					.address-info-bottom {
						font-size: 24rpx;
						color: #999999;
						margin-top: 6rpx;
					}
				}

				.address-righticon {

					image {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}

		.goods {
			display: flex;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 32rpx 30rpx;
			margin-top: 22rpx;

			.goods-left {

				image {
					width: 130rpx;
					height: 130rpx;
				}
			}

			.goods-center {
        flex-grow: 1;
				display: flex;
				flex-direction: column;
				padding: 0rpx 60rpx 0rpx 30rpx;

				.goods-name {
					font-size: 26rpx;
					font-weight: 600;
					color: #333333;
				}

				.goods-spec {
					font-size: 22rpx;
					color: #999999;
					margin-top: 10rpx;
				}

				.goods-num {
					font-size: 22rpx;
					color: #999999;
				}
			}

			.goods-right {
				font-size: 22rpx;
				font-weight: 600;

				.goods-price {
					font-size: 32rpx;
					color: #333333;
				}
			}

		}

		.price {
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 32rpx 30rpx;
			margin-top: 22rpx;

			.price-top {
				display: flex;
				flex-direction: column;
				line-height: 40rpx;
				padding: 10rpx 0rpx;

				.price-top-left {
					font-size: 24rpx;
					color: #666666;
				}

				.price-top-xiaoji {
					display: flex;
					justify-content: space-between;

					.price-top-right {
						font-weight: 600;
						font-size: 22rpx;

						.xiaoji-price {
							font-size: 32rpx;
						}
					}

				}

				.price-top-youhui {
					display: flex;
					justify-content: space-between;

					.youhui-price {
						font-size: 24rpx;
						color: #333333;
					}
				}
			}

			.price-bottom {
				margin-top: 16rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.price-bottom-left {
					font-size: 24rpx;
					font-weight: 600;
					color: #333333;
				}

				.price-bottom-right {
					font-weight: 600;
					font-size: 22rpx;
					color: #F55726;

					.xiaoji-price {
						font-size: 32rpx;
					}
				}
			}
		}

		.order {
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 32rpx 30rpx;
			margin-top: 22rpx;

			.order-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				line-height: 50rpx;

				.order-item-left {
					font-size: 24rpx;
					color: #999999;
				}

				.order-item-right {
					font-size: 24rpx;
					color: #333333;
				}
			}
		}
	}

	.bottom {
		position: fixed;
    bottom: 50rpx;
		width: 100%;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		padding: 22rpx 0rpx;

	}

	.btns {
		display: flex;
		margin-right: 30rpx;

		button {
			border-radius: 24rpx;
			background-color: #FFFFFF;
			margin: 0 20rpx 0 0;
		}

		button::after {
			border: none;
		}

		.cancel {
			font-size: 24rpx;
			color: #333333;
			border: 1rpx solid #999999;
		}

		.pay {
			font-size: 24rpx;
			color: #4e8ef7;
			border: 1rpx solid #4e8ef7;
		}
	}
</style>
